<?php
/**
 * Created by PhpStorm.
 * User: ysfl
 * Date: 2015/11/29
 * Time: 13:38
 * 日程表模块
 */

namespace modules\clents;

use Cross\MVC\Module;

class CalendarModule extends Module
{

    /**
     * 日程表
     *
     * @var string
     */
    protected $tables = 'xrk_user_calendar';
    protected $t_list = 'xrk_user_calendar_list';
    /**
     * 添加日程表
     *
     * @param $data
     * @return text
     */
    function addCalendar($data)
    {
        //检查表单是否为空
        foreach ($data as $key => & $val) {
            if (empty($val)) {
                switch($key)
                {
                    case 'time':
                        $name = "时间";
                        break;
                    case 'mobile':
                        $name = "客户手机";
                        break;
                    case 'text':
                        $name = "任务描述";
                        break;
                    case 'addr':
                        $name = "地址";
                        break;
                    case 'name':
                        $name = "操作员";
                        break;
                    default:
                        $name = "未知错误";
                        break;
                }
                return "请输入：".$name;
                unset($data[$key]);
            }
        }

        //数据写入数据库
        if ($this->link->add($this->tables, $data)){
            return "添加成功！";
        }
        else{
            return "添加失败，请联系管理员";
        }
    }

    /**
     * 日程表列表
     * @return array
     */

    function queryCalendar()
    {
        //返回数据
        return $this->link->getAll($this->tables, '*');
    }

    /**
     * 日程表任务跟踪列表
     * $data:任务编号
     * @return text
     */
    function query_list($data)
    {
        //返回数据
        return $this->link->getAll($this->t_list, '*',array('id' => $data));
    }

    /**
     * 日程表跟踪次数查询
     * $data:任务编号
     * @return int
     */
    function list_num($data)
    {
        //获取当前任务跟踪次数
        $e = $this->link->select('count(*)')
            ->from($this->t_list)
            ->where(array('id' => $data))
            ->stmt()
            ->fetchAll(\PDO::FETCH_ASSOC);

        //返回array的值
        return $e[0]['count(*)'];
    }

    /**
     * 日程表确认
     * $data:
     * @return int
     */
    function query_set($data)
    {

        switch ($data['start']){

            case "ok":

                $sql = $this->link->update($this->tables, array(
                    'start' => 1,
                ), array('entry' => $data['id']));

                $s = "确定成功！";

                break;

            case "set":

                $sql = $this->link->add($this->t_list, array(
                    'id' => $data['id'],
                    'text' => $data['text'],
                    'time' => date("Y-m-d H:i:s"),
                    'name' => $data['name']
                ));

                $s = "更新成功！";

                break;

            case "del":

                $sql = $this->link->update($this->tables, array(
                    'start' => 2,
                ), array('entry' => $data['id']));

                $s = "成功删除！";

                break;

            default :

                $s = "错误的值";

                break;
        }

        if ($sql){
            return $s;
        }
    }
}